package com.hzhc.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.SysDictType;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.mapper.SysDictTypeMapper;
import com.hzhc.service.SysDictTypeService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.SysDictTypeVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-04-07
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {
    @Override
    public List<SysDictTypeVo> getSysDictTypeVoList(Page<SysDictTypeVo> pagination, SysDictTypeVo sysDictTypeVo) {
        IPage<SysDictTypeVo> sysDictTypeList = baseMapper.getSysDictTypeVoList(pagination, sysDictTypeVo);
        return sysDictTypeList.getRecords();
    }

    @Override
    public boolean addDictType(SysDictType sysDictType) {
        sysDictType.setDisable(0).setCreateTime(LocalDateTime.now());
        boolean b = save(sysDictType);
        return b;
    }

    @Override
    public boolean updateDictType(SysDictType sysDictType) {
        sysDictType.setUpdateTime(LocalDateTime.now());
        boolean b = updateById(sysDictType);
        return b;
    }

    @Override
    public boolean delDict(Long id) {
        return baseMapper.deleteDictType(id);
    }

    @Override
    public SysDictType getByCode(String code) {
        List<SysDictType> list = baseMapper.selectList(new LambdaQueryWrapper<SysDictType>()
                .eq(SysDictType::getDisable, 0)
                .eq(SysDictType::getStatus, "ENABLE")
                .eq(SysDictType::getCode, code)
        );
        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public Long getTimeDiff(Long timeDifference) {
        SysDictType diff = getByCode("timeDiff");
        if (diff == null || StrUtil.isBlank(diff.getDescription())) {
            return timeDifference;
        }
        String s = diff.getDescription().replaceAll(" ", "");
        if (NumberUtil.isInteger(s)) {
            return Long.parseLong(s);
        }
        return timeDifference;
    }

    @Override
    public ResultUtil<T> checkDictTypeField(SysDictType type) {
        if (StrUtil.isNotBlank(type.getCode())) {
            int i = count(Wrappers.<SysDictType>lambdaQuery()
                    .ne(type.getId() != null, SysDictType::getId, type.getId())
                    .eq(SysDictType::getCode, type.getCode()).eq(SysDictType::getDisable,0));
            if (i > 0) {
                return ResultUtil.build(ResultUtilCodeEnum.SYSDIC_CODE);
            }
        }
        if (StrUtil.isNotBlank(type.getName())) {
            int i = count(Wrappers.<SysDictType>lambdaQuery()
                    .ne(type.getId() != null, SysDictType::getId, type.getId())
                    .eq(SysDictType::getName, type.getName()).eq(SysDictType::getDisable,0));
            if (i > 0) {
                return ResultUtil.build(ResultUtilCodeEnum.SYSDIC_NAME);
            }
        }
        return ResultUtil.ok();
    }
}
